const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  // return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  return [year, month, day].map(formatNumber).join('-')
}

function date_time(number, format) {
  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number * 1000);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function getUserdata(key) {
  return wx.getStorageSync(key) || {}
}
function getUserArr(key) {
  return wx.getStorageSync(key) || []
}
function setUserdata(key, data) {
  wx.setStorageSync(key, data)
}
function removeUserData(key) {
  wx.removeStorageSync(key) || []
}

function headers() {
  return { "Content-Type": "application/x-www-form-urlencoded", "Cookie": "XDEBUG_SESSION=10632" };
}

function extend(des, src, override) {
  if (src instanceof Array) {
    for (var i = 0, len = src.length; i < len; i++)
      extend(des, src[i], override);
  }
  for (var i in src) {
    if (override || !(i in des)) {
      des[i] = src[i];
    }
  }
  return des;
}

function reqUrl(path, sign, data) {
  var url = 'https://gongzheng.ma.cn/Lapp/' + path + '.html'
  //var url = 'https://s.huabanlu.com/index.php/Lapp/' + path + '.html'
  // var url = 'http://www.api.com/index.php/Lapp/' + path + '.html'
  if (typeof (sign) != "undefined") {
    url = url + '?identity_token=' + this.getUserdata('identity_token')
    for (var i in data) {
      url += '&' + i + '=' + data[i]
    }
  }
  console.log('请求链接！' + url)
  return url
}

function request(url, data, succ, fail, complete) {
  // let odata = {}
  // console.log(this.getUserdata('identity_token'))
  // if (getUserdata) {
  //   odata = { 'identity_token': this.getUserdata('identity_token') };
  // }
  // console.log('identity_token==' + this.getUserdata('identity_token'))

  console.log('请求链接2！' + url)
  wx.request({
    url: reqUrl(url),
    header: headers(),
    method: 'post',
    // data: extend(odata, data),
    data: data,
    success(res) {
      succ(res)
    },
    fail(res) {
      if (fail) fail(res);
      //尝试重启
      wx.reLaunch({
        url: 'pages/index/index'
      })
    },
    complete(res) {
      if (complete) complete(res);
    },
  })
}

module.exports = {
  formatTime: formatTime,
  reqUrl: reqUrl,
  headers: headers,
  request: request,
  extend: extend,
  getUserdata: getUserdata,
  getUserArr: getUserArr,
  setUserdata: setUserdata,
  removeUserData: removeUserData,
  date_time: date_time
}
